<?php
/*
 * Created on May 25, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Upload_files_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;
	/**
	 * @var unknown_type
	 */
	var $category_parent_id  ;
	/**
	 * @var unknown_type
	 */
	var $name_vi  ;
	/**
	 * @var unknown_type
	 */
	var $name_en  ;
	/**
	 * @var unknown_type
	 */
	var $description_vi;
	/**
	 * @var unknown_type
	 */
	var $description_en;
	/**
	 * @var unknown_type
	 */
	var $picture;
	/**
	 * @var unknown_type
	 */
	var $active;
	/*
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
	}

        function upload_files_model()
	{
		parent::__construct();
	}

	function insertGroupUser($user_id, $group_id, $permission_id, $color_id, $active){
		$sql = "INSERT IGNORE INTO user_group (user_id, group_id, permission_id, color_id, active) VALUES ($user_id, $group_id, $permission_id, $color_id, $active)";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	function create($categories){
		if(!is_array($categories))
			$categories = array($categories);

		$sql = "INSERT INTO `gtb_upload_files` " .
				"(
                                    `item_id` ,
                                    `vi_file_name` ,
                                    `en_file_name` ,
                                    `vi_alias` ,
                                    `en_alias` ,
                                    `vi_description` ,
                                    `en_description` ,
                                    `position` ,
                                    `file_type` ,
                                    `item_type` ,
                                    `active` ,
                                    `add_date` ,
                                    `upd_date` ) " .
				"VALUES";
		for($i = 0; $i < count($categories); $i++){
			$category = $categories[$i];
			$sql .= "(" .
                                    intval($category->item_id).", " .
                                    $this->db->escape($category->vi_file_name).", " .
                                    $this->db->escape($category->en_file_name).", " .
                                    $this->db->escape($category->vi_alias).", " .
                                    $this->db->escape($category->en_alias).", " .
                                    $this->db->escape($category->vi_description).", " .
                                    $this->db->escape($category->en_description).", " .
                                    intval($category->position).", " .
                                    intval($category->file_type).", " .
                                    intval($category->item_type).", " .
                                    intval($category->active).", " .
                                    $this->db->escape($category->add_date).", " .
                                    $this->db->escape($category->upd_date).
				"),";

		}
		$sql = substr($sql, 0, strlen($sql)-1);
		$this->db->query($sql);
                //print_r($categories);
               // echo "<br> My record:".  count($categories)." </br>";
		//return $this->db->affected_rows();
		return $this->db->insert_id();
	}

	function update($category){
		 $sql = "UPDATE gtb_files SET " .
				"id_category = " .  intval($category->id_category) . "," .
				"vi_alias = " .  $this->db->escape($category->vi_alias) . "," .
				"en_alias = " .  $this->db->escape($category->en_alias) . ", " .
				"file_name = " .  $this->db->escape($category->file_name) . ", " .
				"active = " .  intval($category->active) . ", " .
				"upd_date = " .  $this->db->escape($category->upd_date) .
				"WHERE id_file = " .intval($category->id_file);
		$this->db->query($sql);
		return $this->db->affected_rows();
	}


	function delete($ids){
		if(!is_array($ids)) $files_name = array($ids);
		$ids = implode(',', $ids);

		$sql = "DELETE FROM gtb_fck_files WHERE id IN ($ids)";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	function getAll($number_record, $offset, $id_category =  0){
		if(!is_array($id_category))
		$sql = "SELECT id as image_id, vi_file_name
				FROM gtb_files " .
				" WHERE id_category= ". intval($id_category). " OR 0 = ". intval($id_category). " ORDER BY id_file DESC" .
				" LIMIT ". intval($offset) ."," . intval($number_record);
		elseif(is_array($id_category)){

			$sql = "SELECT *
				FROM gtb_files " .
				" WHERE id_category IN (". implode(',',$id_category). ") ORDER BY id_file DESC" .
				" LIMIT ". intval($offset) ."," . intval($number_record);

		}
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();

		return $result;
	}

	function getAllByItemId($item_id, $type){

		$sql = "SELECT id as image_id, vi_file_name
				FROM gtb_fck_files " .
				" WHERE item_id = (". intval($item_id). ") AND type='$type'" ;

		$query = $this->db->query($sql);
		$result = $query->result();

		return $result;
	}

	function countAll($id_category){
		$result = null;
		if(!is_array($id_category))
			$sql = "SELECT COUNT(*) as num_record
					FROM gtb_files " .
					"WHERE id_category= ". intval($id_category). " OR 0 = ". intval($id_category);
		elseif(is_array($id_category))
			$sql = "SELECT COUNT(*) as num_record
					FROM gtb_files " .
					" WHERE id_category IN (". implode(',',$id_category). ")";

		$query = $this->db->query($sql);
		$result = $query->result();
       	if($result)
			return $result[0]->num_record;

		return 0;
	}


	function getById($file_id){

		$sql = "SELECT *
				FROM gtb_files
				WHERE id_file = " . intval($file_id);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}

		return null;
	}

        function getMaxPosition(){

		$sql = "SELECT MAX(position) as max_position
				FROM gtb_upload_files";
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0]['max_position'];
		}

		return 0;
	}

        function getFileByType($tour_id, $type = 1, $item_type = 1){

		$sql = "SELECT id as image_id, vi_file_name
                FROM gtb_upload_files " .
		" WHERE item_id = ". intval($tour_id). " AND (file_type=".intval($type)." OR 0 = " . intval($type) .") AND ".
                " item_type = " . intval($item_type);

		$query = $this->db->query($sql);
		//$result = $query->result();

		return $query->result_array();;
	}

        function deleteByName($name){

		$sql = "DELETE
                FROM gtb_upload_files " .
		" WHERE vi_file_name = ". $this->db->escape($name);

		$query = $this->db->query($sql);
		//$result = $query->result();

		return $this->db->affected_rows();
	}
//
}

// EOF

